package com.robotserver.orders.service.impl;

import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.mapper.*;
import com.robotserver.orders.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: liuqi
 * @Date: 2021/4/1 14:44
 * @Description: 监听订单过期
 */
@Service
public class ListenOrdersExpiredServiceImpl implements ListenOrdersExpiredService {

    private Logger logger = LoggerFactory.getLogger(ListenOrdersExpiredServiceImpl.class);

    @Autowired
    private TbOrdersMapper tbOrdersMapper;
    @Autowired
    private TbOrdersRefundMapper tbOrdersRefundMapper;
    @Autowired
    private JdOrdersMapper jdOrdersMapper;
    @Autowired
    private VipOrdersMapper vipOrdersMapper;
    @Autowired
    private PddOrdersMapper pddOrdersMapper;
    @Autowired
    private ElmOrdersMapper elmOrdersMapper;
    @Autowired
    private DyOrdersMapper dyOrdersMapper;

    @Override
    public void listenOrdersExpired(Integer duration) {

        if(duration == null){
            duration = 864000; //如果为空，则默认10天
        }

        try {
            // 获取
            int now = DateUtil.getNowTime();
            int validTime = now-duration;  //删除10天前创建的订单
            //删除京东过期订单
            jdOrdersMapper.deleteOrdersExpired(validTime);
            //删除淘宝过期订单
            tbOrdersMapper.deleteOrdersExpired(validTime);
            //删除唯品会过期订单
            vipOrdersMapper.deleteOrdersExpired(validTime);
            //删除拼多多过期订单
            pddOrdersMapper.deleteOrdersExpired(validTime);
            //删除淘宝维权订单
            tbOrdersRefundMapper.deleteOrdersExpired(validTime);
            //删除饿了么订单
            elmOrdersMapper.deleteOrdersExpired(validTime);
            //删除抖客订单
            dyOrdersMapper.deleteOrdersExpired(validTime);
            logger.info("--监听过期订单删除功能程序启动------完成----");

        }catch (Exception e){
            logger.info("--delOrdersExpired error:{}"+e);
        }

    }
}
